package com.gitee.jjnnzb.designpatterninspring.business.openapi.handler;

import com.gitee.jjnnzb.designpatterninspring.common.bean.CommonException;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jjn
 * @since 2023/8/10 19:47
 */
@RequiredArgsConstructor
@Component
public class DataPushStrategy {
    
    private final List<DataPushTemplate> templates;
    
    private Map<String, DataPushTemplate> strategyMap;
    
    @PostConstruct
    public void init() {
        this.strategyMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(templates)) {
            templates.forEach(dataPushTemplate -> {
                strategyMap.put(dataPushTemplate.getChannelName(), dataPushTemplate);
            });
        }
    }
    
    public void handle(Map<String, Object> map) {
        DataPushTemplate template = strategyMap.get((String) map.get("channel"));
        if (template == null) {
            throw new CommonException("未找到对应处理渠道");
        }
        template.handle0(map);
    }
    
}
